﻿//--------------------------------------------------------------------------------------------------------------------------------------------------------
// <copyright file="EmailService.cs">
//     Copyright © 2010 Oli Francis
//     This source code is subject to the terms and conditions of the MIT license. A copy of the license can be found in the license.txt 
//     file at the root of this distribution. By using this source code in any fashion, you are agreeing to be bound by the terms of the MIT License. 
//     You must not remove this notice from the software.
// </copyright>
// <author>Oli</author>
//-------------------------------------------------------------------------------------------------------------------------------------------------------

namespace TeamBrain.Domain.Services.Email
{
    using System;
    using System.Collections.Generic;

    using TeamBrain.Domain.Model;
    using TeamBrain.Domain.Services.Configuration;
    using TeamBrain.Domain.Services.Email.Templates;

    /// <summary>
    /// Exposes functionality around sending system emails
    /// </summary>
    public class EmailService : IEmailService
    {
        private readonly IEmailTransport transport;
        private readonly IConfiguration configuration;

        /// <summary>
        /// Initializes a new instance of the <see cref="EmailService"/> class.
        /// </summary>
        public EmailService(
            IEmailTransport transport,
            IConfiguration configuration)
        {
            this.transport = transport;
            this.configuration = configuration;
        }

        /// <summary>
        /// Sends a user activation email
        /// </summary>
        public void SendUserActivationEmail(User user)
        {
            var template = new ActivationEmailTemplate();
            template.Session = new Dictionary<string, object>()
                {
                    { "User", user },
                    { "RootUrl", this.configuration.RootUrl }
                };

            template.Initialize();

            var email = this.GetBasicEmail(user);
            email.PlainTextBody = template.TransformText();

            this.transport.Send(email);
        }

        /// <summary>
        /// Sends a password reset email
        /// </summary>
        public void SendPasswordResetEmail(User user)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets an email with basic details from the recipient and config
        /// </summary>
        private SystemEmail GetBasicEmail(User recipient)
        {
            return new SystemEmail
                {
                    ToDisplayName = recipient.FirstName,
                    ToAddress = recipient.EmailAddress,
                    FromDisplayName = this.configuration.EmailFromDisplayName,
                    FromAddress = this.configuration.EmailFromAddress
                };
        }
    }
}